/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package parsers;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author matheus
 */
public class GHXMLParser {

    private Document dom;
    private String nomeArquivo;

    public GHXMLParser() {
    }

    public GHXMLParser(String nomeArquivo) {
        
        this.nomeArquivo = nomeArquivo;
    }

    public void mapXmlFile() {
        //get the factory
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {

            //Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            //File f = new File("RES_21102009_1456_OS_10912695.xml");
            File f = new File(getNomeArquivo());

            if (!f.exists()) {
                System.out.println("Documento não encontrado");
                System.exit(1);
            }

            //parse using builder to get DOM representation of the XML file
            this.setDom(db.parse(f));



        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (SAXException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    public void mapXmlFile(InputStream is) {
        //get the factory
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {

            //Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            //parse using builder to get DOM representation of the XML file
            setDom(db.parse(is));



        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (SAXException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    public ArrayList<String> getResultCodes() {
        
        ArrayList<String> resultCodes = null;
        
        Element docEle = getDom().getDocumentElement();

        NodeList nl = docEle.getElementsByTagName("Result");

        if(nl.getLength() > 0){
            resultCodes = new ArrayList<String>();
        }

        for (int i = 0; i < nl.getLength(); i++) {

            Element el = (Element) nl.item(i);

            NodeList descList = el.getElementsByTagName("Description");

            Element descElement = (Element) descList.item(0);

            NodeList codeList = descElement.getElementsByTagName("Code");

            Element codeElement = (Element) descList.item(0);
            try {
               String value =  getChildTagValue(codeElement, "Value");
               if(!resultCodes.contains(value)){
                   resultCodes.add(value);
               }
            } catch (Exception ex) {
                Logger.getLogger(GHXMLParser.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return resultCodes;

    }

    public ArrayList<String> getTestNames(){
        ArrayList<String> testNames = null;
        
        Element docEle = getDom().getDocumentElement();

        NodeList nl = docEle.getElementsByTagName("Result");

        if(nl.getLength() > 0){
            testNames = new ArrayList<String>();
        }

        for (int i = 0; i < nl.getLength(); i++) {

            Element el = (Element) nl.item(i);

            NodeList testList = el.getElementsByTagName("Test");

            Element testElement = (Element) testList.item(0);

            NodeList descList = testElement.getElementsByTagName("Description");

            Element descElement = (Element) descList.item(0);

            NodeList codeList = descElement.getElementsByTagName("Code");

            Element codeElement = (Element) codeList.item(0);
            try {
               String value =  getChildTagValue(codeElement, "Value");
               if(!testNames.contains(value)){
                   testNames.add(value);
               }
            } catch (Exception ex) {
                Logger.getLogger(GHXMLParser.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return testNames;
    }

    // este método lê e retorna o conteúdo (texto) de uma tag (elemento)
    // filho da tag informada como parâmetro. A tag filho a ser pesquisada
    // é a tag informada pelo nome (string)
    private String getChildTagValue(Element elem, String tagName) throws Exception {
        NodeList children = elem.getElementsByTagName(tagName);
        if (children == null) {
            return null;
        }
        Element child = (Element) children.item(0);
        if (child == null) {
            return null;
        }
        return child.getFirstChild().getNodeValue();
    }

    /**
     * @return the dom
     */
    public Document getDom() {
        return dom;
    }

    /**
     * @param dom the dom to set
     */
    public void setDom(Document dom) {
        this.dom = dom;
    }

    /**
     * @return the nomeArquivo
     */
    public String getNomeArquivo() {
        return nomeArquivo;
    }

    /**
     * @param nomeArquivo the nomeArquivo to set
     */
    public void setNomeArquivo(String nomeArquivo) {
        this.nomeArquivo = nomeArquivo;
    }

   
}
